//
//  InviteController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/16.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "InviteController.h"
#import "InviteContentView.h"
#import "UIButton+BCYImage.h"
#import "InviteObj.h"
#import "BeeGroupListObj.h"
#import "ShareTool.h"
#import "UIImageView+WebCache.h"
#import "InviteContentLargeView.h"

#import "FbeeAPP-Swift.h"

@interface InviteController ()<UIScrollViewDelegate>
{
    NSString *groupId;
    
    UIScrollView *mainView;
    
    UIButton *closeBtn;
    UILabel *infoL;
    BeeGroupListObj *beeObj;
    InviteContentView *contentV;
    UILabel *sloganL;
    
    UIButton *saveBtn;
}

@end

@implementation InviteController

-(instancetype)initWithObj:(BeeGroupListObj *)obj
{
    self = [super init];
    
    if (self) {
        beeObj = obj;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.view.backgroundColor = UIColorFromRGB(0x000000);
    
    [self hiddeBack];
    
    [self buildView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)buildView
{
    self.titleLabel.text = NSLocalizedString(@"tNavBeeGroupInvite", nil);
    [self.largeTitle setTitle:NSLocalizedString(@"tNavBeeGroupInvite", nil)];
    [self.largeTitle setTitleColor:COLOR_WHITE];
    
    UIImageView *bgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT)];
    [bgview setContentScaleFactor:[[UIScreen mainScreen] scale]];
    bgview.contentMode =  UIViewContentModeScaleAspectFill;
    bgview.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    bgview.clipsToBounds  = YES;
    [bgview sd_setImageWithURL:[NSURL URLWithString:beeObj.logoUrl]];
    [self.view addSubview:bgview];
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT);
    [self.view addSubview:effectView];

    
    
    mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT)];
    mainView.delegate = self;
    [self.view addSubview:mainView];
    [mainView addSubview:self.largeTitle];
    
    infoL = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.addText(beeObj.inviteObj.inviteTips).addTextColor(COLOR_WHITE).addFont([UIFont systemFontOfSize:14.0f]);
        [make sizeToFit];
        make.top = self.largeTitle.height + 5;
        make.left = 16;
        [mainView addSubview:make];
    }];
    
    contentV = [[InviteContentView alloc] initWithFrame:CGRectMake(0, 0, 345*UISCALE, 425*UISCALE) obj:beeObj];
    contentV.top = infoL.bottom + 20;
    contentV.centerX = mainView.centerX;
    [mainView addSubview:contentV];
    
    sloganL = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.addTextColor(COLOR_WHITE).addFont([UIFont systemFontOfSize:12.0f]);
        make.addText(@"Fbee.one 10000万糖果区块链社区");
        [make sizeToFit];
        make.top = contentV.bottom + 17;
        make.centerX = mainView.centerX;
        [mainView addSubview:make];
    }];
    
    saveBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(IPHONEWIDTH - 81, StatusHeight, 74, 44));
        make.addFont([UIFont boldSystemFontOfSize:16.0f]);
        make.addTitle(NSLocalizedString(@"mSaveImg", nil));
        make.addTitleColor(COLOR_WHITE,UIControlStateNormal).addTitleColor(FBEE_COLOR_INFO,UIControlStateHighlighted);
        make.addTarget(self,@selector(saveImg));
        [self.view addSubview:make];
    }];
    
    
    closeBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 44, 44));
        make.addImage([UIImage imageNamed:@"nav_close_w"],UIControlStateNormal);
        make.addTarget(self,@selector(close));
        [self.view addSubview:make];
    }];
    [closeBtn setCenterByImageSize:(CGSizeMake(17, 17))];
    
    UIButton *closeNav = [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 44, 44));
        make.addImage([UIImage imageNamed:@"nav_close_b"],UIControlStateNormal);
        make.addTarget(self,@selector(close));
        [self.navigationView addSubview:make];
    }];
    [closeNav setCenterByImageSize:(CGSizeMake(17, 17))];
    
    
//    if ([CommonTool canShowShareTool]) {
        ShareTool *tool = [[ShareTool alloc] initWithFrame:CGRectMake(0, IPHONEHEIGHT-(100 + IPhoneXDel), IPHONEWIDTH, 100 + IPhoneXDel)];
        [self.view addSubview:tool];
        tool.beeObj = beeObj;
        [mainView setContentSize:CGSizeMake(0, sloganL.bottom + 100 + TabbarHeight)];
//    }
}

#pragma - mark scrollView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float alphaY = scrollView.contentOffset.y;
    
    float alpha = alphaY / (scrollView.bounds.size.height/15.0f);
    float alphaH = 1 - alpha*2;
    
    alpha = alpha<0?0:alpha;
    alpha = alpha>1?1:alpha;
    alphaH = alphaH<0?0:alphaH;
    alphaH = alphaH>1?1:alphaH;
    
    [self.navigationView setAlpha:alpha];
    [self.largeTitle setAlpha:alphaH];
}

-(void)close
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([KeyBoardManager sharedManager].animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    });
}

-(void)saveImg
{
    InviteContentLargeView *lView = [[InviteContentLargeView alloc]initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT) obj:beeObj];
    
    UIImage *image = [CommonTool getImageByView:lView];
    //因为需要知道该操作的完成情况，即保存成功与否，所以此处需要一个回调方法image:didFinishSavingWithError:contextInfo:
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

//回调方法
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    NSString *msg = nil ;
    if(error != NULL){
        msg = [NSString stringWithFormat:@"图片保存失败，请检查照片访问权限"] ;
        NSLog(@"error = %@",error);
        [NoticePopHelper showNoticeText:msg];
    }else{
        
        msg = @"保存图片成功" ;
        [NoticePopHelper showNoticeText:msg];
        
    }
}



@end
